import { SearchAction, SearchInitType } from '@/types/store'
import { getLocalHistory } from '@/utils/storage'

const initValue: SearchInitType = {
  //存放推荐的结果
  suggestion: [],
  //存放历史的结果
  history: getLocalHistory(),
  //存放搜索的结果
  resultsObj: {
    page: 0,
    per_page: 10,
    results: [],
    total_count: -1,
  },
}
const search = (
  prevState = initValue,
  action: SearchAction
): SearchInitType => {
  switch (action.type) {
    case 'search/getSuggestionList':
      return {
        ...prevState,
        suggestion: action.payload,
      }
    case 'search/saveHistoryList':
      return {
        ...prevState,
        history: action.payload,
      }
    case 'search/clearHistories':
      return {
        ...prevState,
        history: [],
      }
    case 'search/saveResults':
      return {
        ...prevState,
        resultsObj: action.payload,
      }
    case 'search/clearResObj':
      return {
        ...prevState,
        resultsObj: {
          page: 0,
          per_page: 10,
          results: [],
          total_count: -1,
        },
      }
    default:
      return prevState
  }
}

export default search
